package bg.unisofia.fmi.tis.ui.bean;

import javax.faces.application.FacesMessage;

import bg.unisofia.fmi.tis.model.Role;
import bg.unisofia.fmi.tis.model.User;
import bg.unisofia.fmi.tis.service.ModelService;

public class LoginBean extends BackingBeanBase {
	private String username;
	
	public String actionLogin() {
		
		if (username != null) {
			User user = ModelService.findByName(User.class, username);
			
			if (user == null) {
				addMessage(FacesMessage.SEVERITY_ERROR, "loginForm", "User not found.");
				
				return "";
			}
			
			String navString = "studentDashboard";
			
			if (user.getRole() == Role.TEACHER) {
				navString =  "teacherDashboard";
			}
			
			UserBean userBean = getManagedBean(UserBean.class);
			userBean.update(user);
			
			return navString;
		}
		
		return "";
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}
